package com.jiajia.study.product.service.impl;

import com.jiajia.study.product.entity.BrandEntity;
import com.jiajia.study.product.entity.CategoryEntity;
import com.jiajia.study.product.service.BrandService;
import com.jiajia.study.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiajia.common.utils.PageUtils;
import com.jiajia.common.utils.Query;

import com.jiajia.study.product.dao.CategoryBrandRelationDao;
import com.jiajia.study.product.entity.CategoryBrandRelationEntity;
import com.jiajia.study.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {


    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveInfo(CategoryBrandRelationEntity categoryBrandRelation) {
        BrandEntity brandEntity = brandService.getById(categoryBrandRelation.getBrandId());
        if(null != brandEntity){
            categoryBrandRelation.setBrandName(brandEntity.getName());
        }
        CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelation.getCatelogId());
        if(null != categoryEntity){
            categoryBrandRelation.setCatelogName(categoryEntity.getName());
        }
        this.save(categoryBrandRelation);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateBrandByBrandId(Long brandId, String name) {
        QueryWrapper<CategoryBrandRelationEntity> categoryBrandRelationEntityQueryWrapper = new QueryWrapper<>();
        categoryBrandRelationEntityQueryWrapper.eq("brand_id",brandId);

        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandName(name);
        this.update(categoryBrandRelationEntity,categoryBrandRelationEntityQueryWrapper);

    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateCategoryByCategoryId(Long catId, String name) {
        QueryWrapper<CategoryBrandRelationEntity> categoryBrandRelationEntityQueryWrapper = new QueryWrapper<>();
        categoryBrandRelationEntityQueryWrapper.eq("catelog_id",catId);

        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setCatelogName(name);
        this.update(categoryBrandRelationEntity,categoryBrandRelationEntityQueryWrapper);
    }

    @Override
    public List<CategoryBrandRelationEntity> listByBrandId(Long brandId) {
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("brand_id",brandId);
        List<CategoryBrandRelationEntity> list = this.list(queryWrapper);
        if(list.isEmpty()){
            return new ArrayList<>();
        }
        return list;
    }

    @Override
    public List<BrandEntity> getBrandByCatId(Long catId) {
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catelog_id",catId);
        List<CategoryBrandRelationEntity> list = this.list(queryWrapper);
        List<BrandEntity> brandEntityList = list.stream().map(item -> {

            BrandEntity brandEntity = brandService.getById(item.getBrandId());
            return brandEntity;

        }).collect(Collectors.toList());
        return brandEntityList;
    }

}